package cn.edu.sjtu.software.sjtucontacts.activity;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import cn.edu.sjtu.software.sjtucontacts.R;
import cn.edu.sjtu.software.sjtucontacts.model.Contact;
import cn.edu.sjtu.software.sjtucontacts.model.Preference;
import cn.edu.sjtu.software.sjtucontacts.util.ImageTool;
import cn.edu.sjtu.software.sjtucontacts.util.ListUtil;

public class ShareContactListAdapter extends BaseAdapter{
	private LayoutInflater mInflater;
	private ViewHolder holder;
	private List<Contact> colist;
	private Context context;
	private ListUtil listutil = ListUtil.getInstance();
	private CheckBox cb = null;
	
	public ShareContactListAdapter(Activity activity, Context context, List<Contact> colist){
		this.context = context;
		mInflater = LayoutInflater.from(context);
		this.colist = colist;
		listutil.clearShareList();
	}
			
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return colist.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		final int pos = position;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.contactiteminshare, null);
			holder = new ViewHolder();
			holder.name = (TextView)convertView.findViewById(R.id.shareContactName);
			holder.name.setTextColor(Preference.getInstance().getFontColorOfName());
			//holder.name.setTextSize(Preference.getInstance().getFontSizeOfName());
			holder.phone = (TextView)convertView.findViewById(R.id.shareContactPhone);
			holder.phone.setTextColor(Preference.getInstance().getFontColorOfName());
			//holder.name.setTextSize(Preference.getInstance().getFontSizeOfName());
			holder.icon = (ImageView)convertView.findViewById(R.id.shareContactImg);
			convertView.setTag(holder);
			
//			if()
		} else {
			holder = (ViewHolder)convertView.getTag();
			//setExpanded(mExpanded.get(position));
			
		}
		try {
			//JSONObject jSONObject = friendResult.getJSONObject(position);
			Contact contact = colist.get(pos);
			holder.name.setText(contact.getFirstName()+contact.getLastName());
			holder.phone.setText(contact.getDefaultPhoneNumber());

			String imageUrl = contact.getPortraitUrl();
			holder.icon.setImageBitmap(new ImageTool().genBitmapByImageFile(imageUrl));
			
			cb = (CheckBox)convertView.findViewById(R.id.shareContactCheckBox);
			cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					if(isChecked) {
						listutil.addContactToShareList(pos);
					}
					else {
						listutil.removeContactFromShareList(pos);
					}
				}
			});
//			System.out.println("cb "+cb.getAutoLinkMask());
//			holder.icon.setClickable(true);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return convertView;
	}

	static class ViewHolder{
		TextView name, phone;
		ImageView icon;		
	}
	
	protected void onClickItemListener()
	{
		
	}
}
